package com.zhongya.example.fragment.myview.study1

import android.animation.*
import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.animation.*
import android.widget.Button
import android.widget.LinearLayout
import com.zhongya.example.R
import com.zhongya.example.databinding.ExampleFragmentPropertyAnimationBinding
import com.zhongya.example.databinding.ExampleFragmentViewStudyBinding
import com.zhongya.example.view.interpolator.MyInterpolator
import com.zhongya.example.view.interpolator.MyInterpolator2
import com.zhongya666.library.base.fragment.BaseDbFragment
import com.zhongya666.library.base.fragment.BaseFragment

class PropertyAnimationFragment : BaseDbFragment<ExampleFragmentPropertyAnimationBinding>(R.layout.example_fragment_property_animation) {

    @SuppressLint("ObjectAnimatorBinding")
    override fun initView() {
        mDatabind.btnAdd.setOnClickListener {
//            myChaZhiQi()
//            testAnimatorSet()
//            testPropertyValuesHolder()
            testLayoutTransition(true)
        }

        mDatabind.btnDelete.setOnClickListener {
            testLayoutTransition(false)
        }

        /*
        * layoutTransition
        * */
        val transition = LayoutTransition()
        //入场动画
        val animIn = ObjectAnimator.ofFloat(null,"rotationY",0f,360f,0f)
        transition.setAnimator(LayoutTransition.APPEARING,animIn)
//        transition.setAnimator(LayoutTransition.CHANGE_APPEARING,animIn)
        //出场动画
        val animOut = ObjectAnimator.ofFloat(null,"rotation",0f,90f,0f)
        transition.setAnimator(LayoutTransition.DISAPPEARING,animOut)
//        transition.setAnimator(LayoutTransition.CHANGE_APPEARING,animOut)
        transition.setDuration(3000)
        mDatabind.llContainer.layoutTransition = transition

    }

    override fun createObserver() {
    }

    override fun lazyLoadData() {
    }


    /*
    * 属性动画
    * 自定义插值器
    * */
    fun myChaZhiQi(){
//        val interpolator = MyInterpolator()
        val interpolator = MyInterpolator2()
        val animator = ValueAnimator.ofInt(0,300)
        animator.addUpdateListener {
            val tv = mDatabind.tv
            val curValue = it.animatedValue as Int
            tv.layout(tv.left,curValue,tv.right,tv.height+curValue)
            Log.e("wzy","$curValue")
        }
        animator.duration = 1000
        animator.interpolator = interpolator
        animator.start()
    }

    fun testAnimatorSet(){
        val a1 = ObjectAnimator.ofFloat(mDatabind.btnAdd,"translationX",0f,100f)
        a1.startDelay = 2000
        val animatorset = AnimatorSet()
        animatorset.play(a1)
        animatorset.startDelay = 2000
        animatorset.start()

    }

    /*
    * 属性动画进阶
    * */
    fun testPropertyValuesHolder(){
        val holder1 = PropertyValuesHolder.ofFloat("rotation",60f,-60f,40f,-40f,20f,-20f,10f,-10f,0f)
        val holder2 = PropertyValuesHolder.ofFloat("alpha",0.1f,1f,0.1f,1f)
        val animator = ObjectAnimator.ofPropertyValuesHolder(mDatabind.tv,holder1,holder2)
        animator.duration = 3000
        animator.start()
    }

    /*
    * keyframe
    * */
    fun testKeyframe(){

    }

    /*
    * animateLayoutChanges 属性
    * layoutTransition
    * */
    var i = 0
    fun testLayoutTransition(isAdd : Boolean){
        if (isAdd){
            i++
            val button = Button(requireContext())
            button.text = "button$i"
            val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
            button.layoutParams = params
            mDatabind.llContainer.addView(button,0)
        }else{
            if (i>0){
                mDatabind.llContainer.removeViewAt(0)
                i--
            }
        }


    }




}